package cn.zkrt.iotms.module.iot.service.impl;

import cn.zkrt.iotms.module.iot.dao.DeviceAttributeBDao;
import cn.zkrt.iotms.module.iot.dao.DeviceBDao;
import cn.zkrt.iotms.module.iot.dao.DeviceTypeAttributeBDao;
import cn.zkrt.iotms.module.iot.entity.DeviceAttributeB;
import cn.zkrt.iotms.module.iot.entity.DeviceB;
import cn.zkrt.iotms.module.iot.entity.DeviceTypeAttributeB;
import cn.zkrt.iotms.module.iot.service.DeviceAttributeBService;
import cn.zkrt.iotms.module.iot.service.DeviceTypeAttributeBService;
import cn.zkrt.iotms.module.iot.vo.DeviceTypeAttributeBVO;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (DeviceTypeAttributeB)表服务实现类
 *
 * @author xulinyu
 * @since 2024-09-28 16:31:40
 */
@Service("deviceTypeAttributeBService")
public class DeviceTypeAttributeBServiceImpl extends ServiceImpl<DeviceTypeAttributeBDao,DeviceTypeAttributeB> implements DeviceTypeAttributeBService {
    @Resource
    private DeviceTypeAttributeBDao deviceTypeAttributeBDao;

    private final DeviceAttributeBDao deviceAttributeBDao;

    private final DeviceBDao deviceBDao;

    private final DeviceAttributeBService deviceAttributeBService;

    public DeviceTypeAttributeBServiceImpl(DeviceAttributeBDao deviceAttributeBDao, DeviceBDao deviceBDao, DeviceAttributeBService deviceAttributeBService) {
        this.deviceAttributeBDao = deviceAttributeBDao;
        this.deviceBDao = deviceBDao;
        this.deviceAttributeBService = deviceAttributeBService;
    }

    @Override
    public Page<DeviceTypeAttributeBVO> pageList(Page<DeviceTypeAttributeBVO> page, String typeAttributeName) {
        return deviceTypeAttributeBDao.pageList(page,typeAttributeName);
    }

    @Override
    public Boolean deleteByIds(List<String> idList) {
        //如果关联了设备属性，不能删除
        LambdaQueryWrapper<DeviceAttributeB> w1 = new LambdaQueryWrapper<>();
        w1.in(DeviceAttributeB::getTypeAttributeId, idList);
        Integer count = deviceAttributeBDao.selectCount(w1);
        if (count > 0) {
            throw new RuntimeException("请解除设备类型属性关联");
        }
        return this.removeByIds(idList);
    }

    @Override
    public Boolean add(DeviceTypeAttributeB deviceTypeAttributeB) {
        boolean save = this.save(deviceTypeAttributeB);
        //如果所属的设备类型已经关联了设备
        //新增设备类型属性时关联设备属性
        LambdaQueryWrapper<DeviceB> w1 = new LambdaQueryWrapper<>();
        w1.eq(StringUtils.isNotEmpty(deviceTypeAttributeB.getDeviceTypeId()),DeviceB::getDeviceTypeId,deviceTypeAttributeB.getDeviceTypeId());
        List<DeviceB> deviceList = deviceBDao.selectList(w1);
        if(CollectionUtils.isNotEmpty(deviceList)){
            List<DeviceAttributeB> deviceAttributeBList = Lists.newArrayList();
            deviceList.forEach(x->{
                DeviceAttributeB deviceAttributeB = new DeviceAttributeB();
                deviceAttributeB.setTypeAttributeId(deviceTypeAttributeB.getId());
                deviceAttributeB.setDeviceId(x.getId());
                deviceAttributeBList.add(deviceAttributeB);
            });
            deviceAttributeBService.saveBatch(deviceAttributeBList);
        }
        return save;
    }
}
